package com.voja.mrvica2.request;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;

public class PostRequest extends AsyncTask<Void, Integer, HttpResponse> {
    
    private String          url;
    private Context         context;
    HashMap<String, String> data;
    File                    file;
    
    public PostRequest(Context context, String url, HashMap<String, String> data) {
        this.context = context;
        this.url     = url;
        this.data    = data;
    }
    
    public PostRequest(Context context, String url, HashMap<String, String> data, File file) {
        this.context = context;
        this.url     = url;
        this.data    = data;
        this.file    = file;
    }
    
    @SuppressWarnings("deprecation")
    @Override
    protected HttpResponse doInBackground(Void... args) {
        HttpResponse result = null;
        try {
            HttpClient client = new DefaultHttpClient();
            HttpPost   post   = new HttpPost(url);
            
            // create Mulitpart builder
            MultipartEntityBuilder builder = MultipartEntityBuilder.create();
            builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
            
            // add POST data
            Charset charset = Charset.forName("UTF-8");
            for (Map.Entry<String, String> entry : data.entrySet()) {
            	android.util.Log.v(entry.getKey(), entry.getValue());
                builder.addPart(entry.getKey(), new StringBody(entry.getValue(), charset));
            }
            
            // add File
            if (file != null) {
                builder.addPart("file", new FileBody(file));
            }
            post.setEntity(builder.build());
            
            // execute request
            result = client.execute(post);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        return result;
    }
    
    @Override
    protected void onPostExecute(HttpResponse response) {
        Activity activity = (Activity)context;
        try {
            activity.getClass()
                .getMethod("postResponse", HttpResponse.class)
                .invoke(activity, response);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

}
